/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihomeapp.info;

import be.contribute.common.pojo.Temperature;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.XYChart;

/**
 *
 * @author wimvandecasteele
 */
public class ChartDataProvider {
    
    
    public static List<XYChart.Series> getDayChart(Map<String,List<Double>> pInput, int pStartHour)
    {
        List<XYChart.Series> vSeries = new ArrayList<XYChart.Series>();
        Iterator it = pInput.entrySet().iterator();
        
        String vName;
        List<Double> vList;
        XYChart.Series vChart;
        int vCounter;
        while (it.hasNext()) 
        {
            Map.Entry pairs = (Map.Entry)it.next();
            vName = pairs.getKey().toString();
            vList = (List<Double>) pairs.getValue();
            vChart = new XYChart.Series();
            vChart.setName(vName);
            vCounter = pStartHour;
            for(Double vValue: vList)
            {
                vChart.getData().add(new XYChart.Data(vCounter, vValue));
                vCounter++;
            }
            vSeries.add(vChart);
            
    }
        return vSeries;
    }

    private static ObservableList<XYChart.Data<Date, Number>> convertToChartList(List<Double> pList, Date pStartDate)
    {
        Date vNewTime = pStartDate;
        ObservableList<XYChart.Data<Date, Number>> seriesData = FXCollections.observableArrayList();
        for(Double vValue: pList)
        {
            seriesData.add(new XYChart.Data<Date, Number>(pStartDate, vValue));
            pStartDate = new Date(vNewTime.getTime() + (60000));
        }
        return seriesData;
    }

    
    public static List<Double> convertTemperatureListToTempValueList(List<Temperature> pTemperatureList, int pLimit)
    {
        List<Temperature> vTemperatureList = pTemperatureList;
        int pForLimit = pTemperatureList.size() - pLimit - 1;
        if(pTemperatureList.size() > pLimit)
        {
            vTemperatureList = new ArrayList();
            for(int i = pTemperatureList.size()-1 ; i > pForLimit; i--)
            {
                vTemperatureList.add(pTemperatureList.get(i));
            }
            List<Temperature> tempList = vTemperatureList;
            Collections.reverse(tempList);
        }        
        List<Double> vList = new ArrayList();
        for(Temperature temp: vTemperatureList)
        {
            vList.add(temp.getTempValue());
        }
        return vList;
    }
}

